/*
 *  $Id$

    Copyright (C) 2008  Nathaniel Waisbrot

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package termint.gui.window;

import java.text.Collator;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import termint.brains.PriceWatcher;
import termint.model.PriceModel;
import termint.model.PriceModel.ChangeListener;

/**
 * @author Nathaniel Waisbrot
 *
 */
public class PriceNotes extends Window {
	private final Table table;
	private final PriceModel model = new PriceModel();
	public PriceModel getPriceModel() {
		return model;
	}
	public PriceNotes(Shell parent) {
		super(parent.getDisplay());
		setText("Price notes");
		setSize(450, 300);
		window.setLayout(new FillLayout());

		table = new Table(window, SWT.SINGLE|SWT.BORDER|SWT.VIRTUAL);
		table.addListener(SWT.SetData, new Listener(){
			@Override public void handleEvent(Event event) {
				TableItem item = (TableItem)event.item;
				int index = table.indexOf(item);
				item.setText(model.dataForRow(index));
			}});
		model.addListener(new ChangeListener(){
			@Override public void tableChanged(int rowCount) {
				if (table.isDisposed()) return;
				table.setItemCount(rowCount);
				table.clearAll();
			}});
		
		final TableColumn nameCol = new TableColumn(table, SWT.LEFT);
		nameCol.setText("Name");
		nameCol.setWidth(200);
		
		final TableColumn price1Col = new TableColumn(table, SWT.LEFT);
		price1Col.setText("low price");
		price1Col.setWidth(80);
		
		TableColumn price2Col = new TableColumn(table, SWT.LEFT);
		price2Col.setText("high price");
		price2Col.setWidth(80);
		
		SelectionListener sortListener = new SelectionListener() {
			@Override public void widgetSelected(SelectionEvent e) {sort(e);}
			@Override public void widgetDefaultSelected(SelectionEvent e) {sort(e);}
			private void sort(SelectionEvent e) {
				TableItem[] items = table.getItems();
				Collator collator = Collator.getInstance();
				TableColumn column = (TableColumn) e.widget;
				int index = (column == nameCol ? 0 : (column == price1Col ? 1 : 2));
				for (int i  = 1; i < items.length; i++) {
					String value1 = items[i].getText(index);
					for (int j = 0; j < i; j++) {
						String value2 = items[j].getText(index);
						int comparison;
						if (index == 0)
							comparison = collator.compare(value1, value2);
						else
							comparison = Integer.compare(Integer.parseInt(value1), Integer.parseInt(value2));
						if (comparison < 0) {
							String[] values = {items[i].getText(0), items[i].getText(1), items[i].getText(2)};
							items[i].dispose();
							TableItem item = new TableItem(table, SWT.NONE, j);
							item.setText(values);
							items = table.getItems();
							break;
						}
					}
				}
				table.setSortColumn(column);
			}
		};
		nameCol.addSelectionListener(sortListener);
		price1Col.addSelectionListener(sortListener);
		price2Col.addSelectionListener(sortListener);
		
		table.setHeaderVisible(true);
	}

	public boolean closeEnough(int a, int b) {
		return Math.abs(a-b) < 5;
	}
}
